<?php

/**
 * Controller_user
 */

class Controller_user extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->user    = YOU::loadModel('user');
		$this->model->staff   = YOU::loadModel('staff');
		$this->model->student = YOU::loadModel('student');
		$this->model->group   = YOU::loadModel('group');
	}

	/** 用户首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}

	/** 冻结用户 **/
	public function actionFreeze()
	{

		$user_id = $this->get->user_id;

		$info = array(
		"status" => 2,
		);
		$this->db->do_update("tbl_user", $info, "user_id='{$user_id}'");

		// 跳转
		Helper_Client::location("冻结成功", "index.php?a=user.list");

	}

	/** 解冻用户 **/
	public function actionUnfreeze()
	{

		$user_id = $this->get->user_id;

		$info = array(
		"status" => 1,
		);
		$this->db->do_update("tbl_user", $info, "user_id='{$user_id}'");

		// 跳转
		Helper_Client::location("解冻成功", "index.php?a=user.list");

	}

	/** 用户档案页 **/
	public function actionInfo()
	{
		$user_data = $this->model->user->getInfo($this->user['user_id']);

		switch ($user_data['user_type']){

			case "staff":
				$staff_data = $this->model->staff->getInfo($user_data['type_id']);
				foreach ($staff_data as $key => $value){
					switch ($key){
						case 'staff_sex':
							$staff_data[$key] = $this->sex_ini[$value];
							break;
						case 'staff_party':
							$staff_data[$key] = $this->party_ini[$value];
							break;
						case 'staff_external':
							$staff_data[$key] = $this->staff_external_ini[$value];
							break;

						case 'staff_sign':
							$staff_data[$key] = $this->sign_ini[$value];
							break;
						case 'staff_position':
							$staff_data[$key] = $this->position_ini[$value];
							break;
					}
				}
				$this->tpl->assign("user_data", $user_data);
				$this->tpl->assign("staff_data", $staff_data);
				$this->tpl->display("user/staff.info.tpl.php");
				break;

			case "student":
				$student_data = $this->model->student->getInfo($user_data['type_id']);
				foreach ( $student_data as $key =>$value ) {
					switch ($key){
						case 'student_over':
						case  "is_work":
						case  "is_alone" :
						case  "is_study":
						case  "is_stay" :
						case  "is_help":
						case  "is_martyr":
						case  "is_subsidy" :
						case  "is_left"    :
							$student_data[$key] = $this->yes_ini[$value];
							break;
						case  "student_mode"  :
							$student_data[$key] = $this->mode_ini[$value];
							break;
						case  "student_sex"   :
							$student_data[$key] = $this->sex_ini[$value];
							break;
                        case  "student_disability"   :
                            $student_data[$key] = $this->disability_ini[$value];
                            break;
                        case  "student_registered"   :
                            $student_data[$key] = $this->registered_ini[$value];
                            break;
                        case  "student_classes"   :
                            $student_data[$key] = $this->classes_ini[$value];
                            break;

						case  "grade_id"   :
							$grade_data = $this->db->fetchRow("select grade_name from tbl_grade_up WHERE grade_id = '{$value}' AND is_now =1") ;
							$student_data[$key] = $grade_data['grade_name'];
							break;
						case  "class_id"   :
							$class_data = $this->db->fetchRow("select class_name from tbl_class_up WHERE class_id = '{$value}' AND is_now =1") ;
							$student_data[$key] = $class_data['class_name'];
							break;
					}
				}
				$this->tpl->assign("user_data", $user_data);
				$this->tpl->assign("student_data", $student_data);
				$this->tpl->display("user/student.info.tpl.php");
				break;


		}



	}

	/** 用户修改 **/
	public function actionUpdate()
	{


		$user_id = $this->get->user_id;

		if ($_POST){

			$start_time = strtotime($this->post->start_time);
			$end_time   = strtotime($this->post->end_time);


			$info = array(
			"start_time"  => $start_time,
			"end_time"    => $end_time,
			"update_time" => date("Y-m-d H:i:s"),
			);
			$this->db->do_update("tbl_user", $info, "user_id='{$user_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=user.list");
		}


		$user_data = $this->model->user->getInfo($user_id);
		if (empty($user_data)){
			Helper_Client::alert("用户不存在");
		}
		$user_name = $this->model->user->getNameById($user_id);


		$user_data['type_note']  = $this->user_type_ini[$user_data['user_type']];
		$user_data['start_time'] = date("Y-m-d", $user_data['start_time']);
		$user_data['end_time']   = date("Y-m-d", $user_data['end_time']);

		$this->tpl->assign("user_data", $user_data);
		$this->tpl->assign("user_name", $user_name);
		$this->tpl->display("user/user.update.tpl.php");
	}


	/** 重置密码 **/
	public function actionReset()
	{
		$user_id = $this->get->user_id;

		// 根据用户类型取得不同的初始密码
		$user_password = $this->model->user->getPass($user_id);
		$user_password = md5($user_password);

		$info = array(
		"user_password" => $user_password,
		"update_time"   => date("Y-m-d H:i:s"),
		);
		$this->db->do_update("tbl_user", $info, "user_id='{$user_id}'");

		// 跳转
		Helper_Client::location("重置成功", "index.php?a=user.update&user_id={$user_id}");

	}


	/** 用户登录 **/
	public function actionLogin()
	{
		ini_set("display_errors", "on");

		if ($_POST){
			$user_name     = $this->post->user_name;
			$user_password = $this->post->user_password;
			$user_type     = $this->post->user_type;
			$is_remember   = $this->post->is_remember;


			$s_hour = date("YmdH");
			// 1小时内密码错误次数超过3次
			$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_user_try` WHERE try_username='{$user_name}' AND try_hour='{$s_hour}'");
			$try_count = $row['count'];
			if ($try_count >=5){
				Helper_Client::alert("密码错误次数太多，请1小时后登录");
			}

			$user_data = $this->db->fetchRow("SELECT * FROM `tbl_user` WHERE user_name='{$user_name}'");

			if (empty($user_data)){
				Helper_Client::alert("用户名错误");
			}
			if ($user_data['status'] !='1'){
				Helper_Client::alert("暂时不能登录，请等待为您解锁");
			}

			$user = $this->auth->dofun("login", array($user_name, $user_password));


			if ($user) {
				// 如果第一次登录 ，登陆后跳转到修改密码页面
				// 修改最后登录信息
				$info = array(
				"last_login"  => date("Y-m-d H:i:s")
				);

				$this->db->do_update("tbl_user", $info, "user_name='{$user_name}'");
				// 跳转
				Helper_Client::reurl("index.php?a=default.index");exit();
			} else {

				if ($this->auth->error == "-9"){
					
					$try_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

					// 记录用户名密码不匹配的次数
					$info = array(
					"try_username" => $user_name,
					"try_password" => $user_password,
					"try_ip"       => $try_ip,
					"try_hour"     => $s_hour,
					"status"       => 1,
					"create_time"  => date("Y-m-d H:i:s"),
					"update_time"  => "0000-00-00 00:00:00",
					);

					$this->db->do_insert("tbl_user_try", $info);

				}

				Helper_Client::alert($this->auth->msg);
			}

		}

		$this->tpl->display("user/user.login.tpl.php");
	}

	/** 用户登出 **/
	public function actionLogout()
	{
		$this->auth->logout();
		// 跳转到登录页面
		Helper_Client::location("退出成功", "index.php?a=user.login");
	}

	/** 修改密码 **/
	public function actionPassword()
	{
		// 用户 登陆检测

		// 表单提交
		if ($_POST) {
			$old_password   = $this->post->old_password;
			$new_password   = $this->post->new_password;
			$new_passwordre = $this->post->new_passwordre;


			$user_data = $this->db->fetchRow("SELECT user_password FROM `tbl_user` WHERE user_id='{$this->user['user_id']}'");

			if ($user_data['user_password'] != md5($old_password)){
				Helper_Client::alert("旧密码输错");
			}
			if ($new_password != $new_passwordre){
				Helper_Client::alert("新密码两次输入不一致");
			}


			// 修改数据库
			$info = array(
			"user_password"  => md5($new_password),
			"update_time"    => date("Y-m-d H:i:s"),
			);

			$this->db->do_update("tbl_user", $info, "user_id='{$this->user['user_id']}'");


			Helper_Client::location("密码修改成功,请重新登录", "index.php?a=user.logout");
		}

		$this->tpl->display("user/user.password.tpl.php");
	}

	/** 用户档案修改 **/
	public function actionInfoUpdate()
	{

		if ($this->user['user_type'] == "admin"){
			Helper_Client::alert("管理员无此功能");
		}


		if ($this->user['user_type'] == "staff"){

			if ($_POST){
				$staff_name = $this->post->staff_name;
				$staff_usedname = $this->post->staff_usedname;
				$staff_sex = $this->post->staff_sex;
				$staff_number = $this->post->staff_number;
				$staff_area = $this->post->staff_area;
				$staff_idtype = $this->post->staff_idtype;//证件类型
				$staff_card = $this->post->staff_card;
				$staff_birth = $this->post->staff_birth;
				$staff_home = $this->post->staff_home;
				$staff_city = $this->post->staff_city;
				$staff_nation = $this->post->staff_nation;
				$staff_party = $this->post->staff_party;
				$staff_marry = $this->post->staff_marry;
				$staff_health = $this->post->staff_health;
				$start_time = $this->post->start_time;
				$end_time = $this->post->end_time;
				$staff_orignal = $this->post->staff_orignal;
				$staff_external = $this->post->staff_external;//leibie
				$staff_nature = $this->post->staff_nature;//zaibian
				$staff_style = $this->post->staff_style;
				$staff_signed = $this->post->staff_signed;
				$staff_normal = $this->post->staff_normal;
				$staff_skill = $this->post->staff_skill;
				$staff_certificate = $this->post->staff_certificate;
				$staff_info = $this->post->staff_info;
				$staff_free = $this->post->staff_free;
				$staff_basic = $this->post->staff_basic;
				$staff_basic_start = $this->post->staff_basic_start;
				$staff_basic_end = $this->post->staff_basic_end;
				$staff_super = $this->post->staff_super;
				$staff_bone = $this->post->staff_bone;
				$staff_mental = $this->post->staff_mental;
				$staff_sign = $this->post->staff_sign;//zhuangtai
				$staff_ctime = $this->post->staff_ctime;//diaodong
				$staff_party_date = $this->post->staff_party_date;
				$staff_addr = $this->post->staff_addr;
				$staff_fix = $this->post->staff_fix;
				$staff_tel = $this->post->staff_tel;
				$staff_position = $this->post->staff_position;//zhicheng
				$staff_educate = $this->post->staff_educate;
				$staff_school = $this->post->staff_school;
				$staff_graduate_time = $this->post->staff_graduate_time;
				$staff_remark = $this->post->staff_remark;



				$info = array(
				"staff_name" => $staff_name,
				"staff_usedname" => $staff_usedname,
				"staff_sex" => $staff_sex,
				"staff_number" => $staff_number,
				"staff_area" => $staff_area,
				"staff_idtype" => $staff_idtype,//证件类型
				"staff_card" => $staff_card,
				"staff_birth" => $staff_birth,
				"staff_home" => $staff_home,
				"staff_city" => $staff_city,
				"staff_nation" => $staff_nation,
				"staff_party" => array_search($staff_party,$this->party_ini),
				"staff_marry" => $staff_marry,
				"staff_health" => $staff_health,
				"start_time" => $start_time,
				"end_time" => $end_time,
				"staff_orignal" => $staff_orignal,
				"staff_external" => array_search($staff_external,$this->staff_external_ini),//leibiearray_flip($this->party_ini)[$staff_party]
				"staff_nature" => $staff_nature,//zaibian
				"staff_style" => $staff_style,
				"staff_signed" => $staff_signed,
				"staff_normal" => $staff_normal,
				"staff_skill" => $staff_skill,
				"staff_certificate" => $staff_certificate,
				"staff_info" => $staff_info,
				"staff_free" => $staff_free,
				"staff_basic" => $staff_basic,
				"staff_basic_start" => $staff_basic_start,
				"staff_basic_end" => $staff_basic_end,
				"staff_super" => $staff_super,
				"staff_bone" => $staff_bone,
				"staff_mental" => $staff_mental,
				"staff_sign" => array_search($staff_sign,$this->sign_ini),//zhuangtai
				"staff_ctime" => $staff_ctime,//diaodong
				"staff_party_date" => $staff_party_date,
				"staff_addr" => $staff_addr,
				"staff_fix" => $staff_fix,
				"staff_tel" => $staff_tel,
				"staff_position" => array_search($staff_position,$this->position_ini),//zhicheng
				"staff_educate" => $staff_educate,
				"staff_school" => $staff_school,
				"staff_graduate_time" => $staff_graduate_time,
				"staff_remark" => $staff_remark,
				"update_time" => date("Y-m-d H:i:s")
				);
				$this->db->do_update("tbl_staff", $info, "staff_id='{$this->user['type_id']}'");

				// 跳转
				Helper_Client::location("修改成功", "index.php?a=user.info");

			}

			$staff_data = $this->model->staff->getInfo($this->user['type_id']);
			foreach ($staff_data as $key => $value){
				switch ($key){
					//                    case 'staff_sex':
					//                        $staff_data[$key] = $this->sex_ini[$value];
					//                        break;
					case 'staff_party':
						$staff_data[$key] = $this->party_ini[$value];
						break;
					case 'staff_external':
						$staff_data[$key] = $this->staff_external_ini[$value];
						break;

					case 'staff_sign':
						$staff_data[$key] = $this->sign_ini[$value];
						break;
					case 'staff_position':
						$staff_data[$key] = $this->position_ini[$value];
						break;
				}
			}
			//var_dump($staff_data);
			$this->tpl->assign("staff_data", $staff_data);
		}


		if ($this->user['user_type'] == "student"){

			if ($_POST){
				$student_tel   = $this->post->student_tel;
				$student_addr  = $this->post->student_addr;
				$info = array(
				"student_mode"            =>$this->post->student_mode=='寄宿生'?1:0,
				"student_name"            => $this->post->student_name,
				"student_school"          => $this->post->student_school,
				"student_card"            => $this->post->student_card,
				"student_sex"             => $this->post->student_sex=='男'?1:0,
				"student_no"              => $this->post->student_no,
				"student_register"       => $this->post->student_register,//
				"student_office"         => $this->post->student_office,
				"student_islocal"        => $this->post->student_islocal,
				"student_over"           => $this->post->student_over=='是'?1:0,
				"student_variation"      => $this->post->student_variation,//biandong
				"student_ctime"          => $this->post->student_ctime,
				"student_remarks"        => $this->post->student_remarks,
				"student_native"         => $this->post->student_native,//jiguan
				"student_nation"         => $this->post->student_nation,//
				"student_addr"           => $this->post->student_addr, // add 20161105
				"student_tel"            => $this->post->student_tel,
				"student_sname"          => $this->post->student_sname,//xuexiaoming
				"student_snumber"        => $this->post->student_snumber,// no
				"student_birth"          => $this->post->student_birth,//
				"student_city"           => $this->post->student_city,//place
				"student_country"        => $this->post->student_country,
				"student_idtype"         => $this->post->student_idtype,
				"grade_id"               => $this->post->grade_id,//                           nianji
				"student_china"          => $this->post->student_china,
				"class_id"               => $this->post->class_id,//                    banji
				"student_politics"       => $this->post->student_politics,//nature
				"student_health"         => $this->post->student_health,
				"student_register_place" => $this->post->student_register_place,//hukou suozai
				"student_registered"     => $this->post->student_registered=='农村'?1:2,//hukou xingzhi        aaaaaaaaa
				"student_start_time"     => $this->post->student_start_time,//ruxue date
				"student_now_addr"       => $this->post->student_now_addr,
				"student_way"            => $this->post->student_way,
				"student_tel_addr"       => $this->post->student_tel_addr,
				"student_home_addr"      => $this->post->student_home_addr,
				"student_mail"           => $this->post->student_mail,
				"is_work"                => $this->post->is_work=='是'?1:0,
				"is_alone"               => $this->post->is_alone=='是'?1:0,
				"is_study"               => $this->post->is_study=='是'?1:0,//aaa
				"is_stay"                => $this->post->is_stay=='是'?1:0,
				"is_help"                => $this->post->is_help=='是'?1:0,
				"is_martyr"              => $this->post->is_martyr=='是'?1:0,
				"is_subsidy"             => $this->post->is_subsidy=='是'?1:0,
				"student_classes"        => array_search($this->post->student_classes,$this->classes_ini),// jiudu fangshiaaa
				"is_left"                => $this->post->is_left=='是'?1:0,
				"student_help_no"        => $this->post->student_help_no,
				"student_class_no"       => $this->post->student_class_no,
				"student_from"           => $this->post->student_from,
				"student_read"           => $this->post->student_read,//suiban
				"student_disability"     => array_search($this->post->student_disability,$this->disability_ini),//
				"student_parent"         => $this->post->student_parent,
				"student_home_relation"  => $this->post->student_home_relation,
				"student_home_work"      => $this->post->student_home_work,
				"student_parent_addr"    => $this->post->student_parent_addr,
				"student_ptel"           => $this->post->student_ptel,
				"student_parent_city"    => $this->post->student_parent_city,
				"update_time"  => date("Y-m-d H:i:s")
				);
				$this->db->do_update("tbl_student", $info, "student_id='{$this->user['type_id']}'");
				// 跳转
				Helper_Client::location("修改成功", "index.php?a=user.info");

			}

			$student_data = $this->model->student->getInfo($this->user['type_id']);
			foreach ( $student_data as $key =>$value ) {
				switch ($key){
					case 'student_over':
					case  "is_work":
					case  "is_alone" :
					case  "is_study":
					case  "is_stay" :
					case  "is_help":
					case  "is_martyr":
					case  "is_subsidy" :
					case  "is_left"    :
						$student_data[$key] = $this->yes_ini[$value];
						break;
					case  "student_mode"  :
						$student_data[$key] = $this->mode_ini[$value];
						break;
						//                    case  "student_sex"   :
						//                        $student_data[$key] = $this->sex_ini[$value];
						//                        break;
				}
			}
			$this->tpl->assign("student_data", $student_data);
		}


		$this->tpl->display("user/{$this->user['user_type']}.update.tpl.php");

	}

    /** 用户档案修改在userlist里的修改 **/
    public function actionInfoUpdate2()
    {
        $uid = $this->get->user_id;
        $user = $this->model->user->getInfo($uid);

        if ($user['user_type'] == "staff"){

            if ($_POST){
                $staff_name = $this->post->staff_name;
                $staff_usedname = $this->post->staff_usedname;
                $staff_sex = $this->post->staff_sex;
                $staff_number = $this->post->staff_number;
                $staff_area = $this->post->staff_area;
                $staff_idtype = $this->post->staff_idtype;//证件类型
                $staff_card = $this->post->staff_card;
                $staff_birth = $this->post->staff_birth;
                $staff_home = $this->post->staff_home;
                $staff_city = $this->post->staff_city;
                $staff_nation = $this->post->staff_nation;
                $staff_party = $this->post->staff_party;
                $staff_marry = $this->post->staff_marry;
                $staff_health = $this->post->staff_health;
                $start_time = $this->post->start_time;
                $end_time = $this->post->end_time;
                $staff_orignal = $this->post->staff_orignal;
                $staff_external = $this->post->staff_external;//leibie
                $staff_nature = $this->post->staff_nature;//zaibian
                $staff_style = $this->post->staff_style;
                $staff_signed = $this->post->staff_signed;
                $staff_normal = $this->post->staff_normal;
                $staff_skill = $this->post->staff_skill;
                $staff_certificate = $this->post->staff_certificate;
                $staff_info = $this->post->staff_info;
                $staff_free = $this->post->staff_free;
                $staff_basic = $this->post->staff_basic;
                $staff_basic_start = $this->post->staff_basic_start;
                $staff_basic_end = $this->post->staff_basic_end;
                $staff_super = $this->post->staff_super;
                $staff_bone = $this->post->staff_bone;
                $staff_mental = $this->post->staff_mental;
                $staff_sign = $this->post->staff_sign;//zhuangtai
                $staff_ctime = $this->post->staff_ctime;//diaodong
                $staff_party_date = $this->post->staff_party_date;
                $staff_addr = $this->post->staff_addr;
                $staff_fix = $this->post->staff_fix;
                $staff_tel = $this->post->staff_tel;
                $staff_position = $this->post->staff_position;//zhicheng
                $staff_educate = $this->post->staff_educate;
                $staff_school = $this->post->staff_school;
                $staff_graduate_time = $this->post->staff_graduate_time;
                $staff_remark = $this->post->staff_remark;



                $info = array(
                    "staff_name" => $staff_name,
                    "staff_usedname" => $staff_usedname,
                    "staff_sex" => $staff_sex,
                    "staff_number" => $staff_number,
                    "staff_area" => $staff_area,
                    "staff_idtype" => $staff_idtype,//证件类型
                    "staff_card" => $staff_card,
                    "staff_birth" => $staff_birth,
                    "staff_home" => $staff_home,
                    "staff_city" => $staff_city,
                    "staff_nation" => $staff_nation,
                    "staff_party" => array_search($staff_party,$this->party_ini),
                    "staff_marry" => $staff_marry,
                    "staff_health" => $staff_health,
                    "start_time" => $start_time,
                    "end_time" => $end_time,
                    "staff_orignal" => $staff_orignal,
                    "staff_external" => array_search($staff_external,$this->staff_external_ini),//leibiearray_flip($this->party_ini)[$staff_party]
                    "staff_nature" => $staff_nature,//zaibian
                    "staff_style" => $staff_style,
                    "staff_signed" => $staff_signed,
                    "staff_normal" => $staff_normal,
                    "staff_skill" => $staff_skill,
                    "staff_certificate" => $staff_certificate,
                    "staff_info" => $staff_info,
                    "staff_free" => $staff_free,
                    "staff_basic" => $staff_basic,
                    "staff_basic_start" => $staff_basic_start,
                    "staff_basic_end" => $staff_basic_end,
                    "staff_super" => $staff_super,
                    "staff_bone" => $staff_bone,
                    "staff_mental" => $staff_mental,
                    "staff_sign" => array_search($staff_sign,$this->sign_ini),//zhuangtai
                    "staff_ctime" => $staff_ctime,//diaodong
                    "staff_party_date" => $staff_party_date,
                    "staff_addr" => $staff_addr,
                    "staff_fix" => $staff_fix,
                    "staff_tel" => $staff_tel,
                    "staff_position" => array_search($staff_position,$this->position_ini),//zhicheng
                    "staff_educate" => $staff_educate,
                    "staff_school" => $staff_school,
                    "staff_graduate_time" => $staff_graduate_time,
                    "staff_remark" => $staff_remark,
                    "update_time" => date("Y-m-d H:i:s")
                );
                $this->db->do_update("tbl_staff", $info, "staff_id='{$user['type_id']}'");

                // 跳转
                Helper_Client::location("修改成功", "index.php?a=user.list");

            }

            $staff_data = $this->model->staff->getInfo($user['type_id']);
            foreach ($staff_data as $key => $value){
                switch ($key){
                    //                    case 'staff_sex':
                    //                        $staff_data[$key] = $this->sex_ini[$value];
                    //                        break;
                    case 'staff_party':
                        $staff_data[$key] = $this->party_ini[$value];
                        break;
                    case 'staff_external':
                        $staff_data[$key] = $this->staff_external_ini[$value];
                        break;

                    case 'staff_sign':
                        $staff_data[$key] = $this->sign_ini[$value];
                        break;
                    case 'staff_position':
                        $staff_data[$key] = $this->position_ini[$value];
                        break;
                }
            }
            //var_dump($staff_data);
            $this->tpl->assign("staff_data", $staff_data);
        }


        if ($user['user_type'] == "student"){

            if ($_POST){
                $student_tel   = $this->post->student_tel;
                $student_addr  = $this->post->student_addr;
                $info = array(
                    "student_mode"            =>$this->post->student_mode=='寄宿生'?1:0,
                    "student_name"            => $this->post->student_name,
                    "student_school"          => $this->post->student_school,
                    "student_card"            => $this->post->student_card,
                    "student_sex"             => $this->post->student_sex=='男'?1:0,
                    "student_no"              => $this->post->student_no,
                    "student_register"       => $this->post->student_register,//
                    "student_office"         => $this->post->student_office,
                    "student_islocal"        => $this->post->student_islocal,
                    "student_over"           => $this->post->student_over=='是'?1:0,
                    "student_variation"      => $this->post->student_variation,//biandong
                    "student_ctime"          => $this->post->student_ctime,
                    "student_remarks"        => $this->post->student_remarks,
                    "student_native"         => $this->post->student_native,//jiguan
                    "student_nation"         => $this->post->student_nation,//
                    "student_addr"           => $this->post->student_addr, // add 20161105
                    "student_tel"            => $this->post->student_tel,
                    "student_sname"          => $this->post->student_sname,//xuexiaoming
                    "student_snumber"        => $this->post->student_snumber,// no
                    "student_birth"          => $this->post->student_birth,//
                    "student_city"           => $this->post->student_city,//place
                    "student_country"        => $this->post->student_country,
                    "student_idtype"         => $this->post->student_idtype,
                    "grade_id"               => $this->post->grade_id,//                           nianji
                    "student_china"          => $this->post->student_china,
                    "class_id"               => $this->post->class_id,//                    banji
                    "student_politics"       => $this->post->student_politics,//nature
                    "student_health"         => $this->post->student_health,
                    "student_register_place" => $this->post->student_register_place,//hukou suozai
                    "student_registered"     => $this->post->student_registered=='农村'?1:2,//hukou xingzhi        aaaaaaaaa
                    "student_start_time"     => $this->post->student_start_time,//ruxue date
                    "student_now_addr"       => $this->post->student_now_addr,
                    "student_way"            => $this->post->student_way,
                    "student_tel_addr"       => $this->post->student_tel_addr,
                    "student_home_addr"      => $this->post->student_home_addr,
                    "student_mail"           => $this->post->student_mail,
                    "is_work"                => $this->post->is_work=='是'?1:0,
                    "is_alone"               => $this->post->is_alone=='是'?1:0,
                    "is_study"               => $this->post->is_study=='是'?1:0,//aaa
                    "is_stay"                => $this->post->is_stay=='是'?1:0,
                    "is_help"                => $this->post->is_help=='是'?1:0,
                    "is_martyr"              => $this->post->is_martyr=='是'?1:0,
                    "is_subsidy"             => $this->post->is_subsidy=='是'?1:0,
                    "student_classes"        => array_search($this->post->student_classes,$this->classes_ini),// jiudu fangshiaaa
                    "is_left"                => $this->post->is_left=='是'?1:0,
                    "student_help_no"        => $this->post->student_help_no,
                    "student_class_no"       => $this->post->student_class_no,
                    "student_from"           => $this->post->student_from,
                    "student_read"           => $this->post->student_read,//suiban
                    "student_disability"     => array_search($this->post->student_disability,$this->disability_ini),//
                    "student_parent"         => $this->post->student_parent,
                    "student_home_relation"  => $this->post->student_home_relation,
                    "student_home_work"      => $this->post->student_home_work,
                    "student_parent_addr"    => $this->post->student_parent_addr,
                    "student_ptel"           => $this->post->student_ptel,
                    "student_parent_city"    => $this->post->student_parent_city,
                    "update_time"  => date("Y-m-d H:i:s")
                );
                $this->db->do_update("tbl_student", $info, "student_id='{$user['type_id']}'");
                // 跳转
                Helper_Client::location("修改成功", "index.php?a=user.list");

            }

            $student_data = $this->model->student->getInfo($user['type_id']);
            foreach ( $student_data as $key =>$value ) {
                switch ($key){
                    case 'student_over':
                    case  "is_work":
                    case  "is_alone" :
                    case  "is_study":
                    case  "is_stay" :
                    case  "is_help":
                    case  "is_martyr":
                    case  "is_subsidy" :
                    case  "is_left"    :
                        $student_data[$key] = $this->yes_ini[$value];
                        break;
                    case  "student_mode"  :
                        $student_data[$key] = $this->mode_ini[$value];
                        break;
                    //                    case  "student_sex"   :
                    //                        $student_data[$key] = $this->sex_ini[$value];
                    //                        break;
                }
            }
            $this->tpl->assign("student_data", $student_data);
        }


        $this->tpl->display("user/{$user['user_type']}.update.tpl.php");

    }
	/** 添加用户 **/
	public function actionAdd()
	{
		if ($_POST){
			$user_name     = $this->post->user_name;
			$user_password = $this->post->user_password;
			$user_type     = $this->post->user_type;
			$group_ids     = $this->post->group_ids;
			$start_time    = $this->post->start_time;
			$end_time      = $this->post->end_time;


			if (empty($user_name)){
				Helper_Client::alert("用户名不能为空");
			}
			if (empty($user_password)){
				Helper_Client::alert("用户密码不能为空");
			}
			if (empty($user_type)){
				Helper_Client::alert("请选择用户类型");
			}
			if (empty($group_ids)){
				Helper_Client::alert("请选择用户组");
			}

			$user_pms = $this->model->group->getPms($group_ids);
			$user_group = serialize($group_ids);

			// 添加到成员表
			if ($this->user_type_ini[$user_type] && !empty($this->user_type_ini[$user_type])){

				if ($user_type == "admin" || $user_type == "repair"){

					$info = array(
					"user_name"     => $user_name,
					"user_password" => $user_password,
					"user_group"    => $user_group,
					"user_pms"      => $user_pms,
					"start_time"    => $start_time,
					"end_time"      => $end_time,
					);

					$this->model->user->add($user_type, $info);
				}



			}


			// 跳转
			Helper_Client::location("添加用户成功", "index.php?a=user.list");
		}


		$group_lists = $this->model->group->getAll();
		$start_time = date("Y-m-d", strtotime("-1 day"));
		$end_time = date("Y-m-d", strtotime("+20 year"));

		$this->tpl->assign("start_time", $start_time);
		$this->tpl->assign("end_time", $end_time);
		$this->tpl->assign("group_lists", $group_lists);
		$this->tpl->display("user/user.add.tpl.php");
	}

	/** 用户列表 **/
	public function actionList()
	{
        $class_id = $this->get->class_id;
        $type = $this->get->type;
        $name = $this->get->name;
        $true_name = $this->get->true_name;
        $where = " 1=1 ";
        if(!empty($name)){
            $where .= " AND user_name like '%{$name}%' ";
        }
        if(!empty($type)){
            $where .= " AND user_type = '{$type}'";
        }
        if(!empty($true_name)){
            if($type=='student'){
                $data_lists = $this->db->fetchAll("select student_id from tbl_student WHERE student_name LIKE '%{$true_name}%'");
                $data = array();
                foreach ($data_lists as $key => $value){
                    $data[] = $value['student_id'];
                }
                $data = implode(",",$data);
            }else{//老师的type_id
                $data_lists = $this->db->fetchAll("select staff_id from tbl_staff WHERE staff_name LIKE '%{$true_name}%'");
                $data = array();
                foreach ($data_lists as $key => $value){
                    $data[] = $value['staff_id'];
                }
                $data = implode(",",$data);
            }
            if(empty($data)) $data = 0;
            $where .= " AND type_id in ({$data})";
        }
        if(!empty($class_id)){
            if($type=='student'){
                $data_lists = $this->db->fetchAll("select student_id from tbl_student WHERE class_id = '{$class_id}'");
                $data = array();
                foreach ($data_lists as $key => $value){
                    $data[] = $value['student_id'];
                }
                $data = implode(",",$data);
                if(empty($data)) $data = 0;
                $where .= " AND type_id in ({$data})";
            }

        }
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_user` WHERE {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$user_lists = $this->db->fetchAll("SELECT * FROM `tbl_user`WHERE {$where} ORDER BY user_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($user_lists as $key => $value){
            if($value['user_type']=='student'){
                $row = $this->db->fetchRow("SELECT * FROM `tbl_student` WHERE student_id = {$value['type_id']}");
                $class_name = $this->db->fetchRow("SELECT class_name FROM `tbl_class_up` WHERE is_now=1 AND class_id='{$row['class_id']}'");
                $user_lists[$key]['class'] = $class_name['class_name'];
            }
            $user_lists[$key]['type_note'] = $this->user_type_ini[$value['user_type']];
            //真名
            $user_lists[$key]['true_name'] = $this->model->user->getNameById($value['user_id']);
			// 用户组名称
			$user_lists[$key]['user_group'] = $this->model->group->getNames($value['user_group']);
			//$user_lists[$key]['user_name']  = $this->model->user->getNameById($value['user_id']);
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
        $classes = $this->db->fetchAll("SELECT * FROM `tbl_class_up` WHERE is_now=1");

        $this->tpl->assign("classes", $classes);
        $this->tpl->assign("type", $type);
		$this->tpl->assign("user_lists", $user_lists);
		$this->tpl->display("user/user.list.tpl.php");
	}

    /** 学生状态修改 **/
    public function actionstatus(){
        $status = $this->get->status;
        if($status==1){
            $this->db->query("UPDATE tbl_user SET status='1' WHERE user_type='student'");
            Helper_Client::alert("启用成功");
        }elseif($status==0){
            $this->db->query("UPDATE tbl_user SET status='0' WHERE user_type='student'");
            Helper_Client::alert("停用成功");
        }
        
        $this->tpl->display("user/user.list.tpl.php");
    }
	/** 用户权限 **/
	public function actionPms ()
	{
		$group_id  = (int)$this->get->group_id;

		$user_id   = (int)$this->get->user_id;

		$user_data = $this->db->fetchRow("SELECT user_pms FROM `tbl_user` WHERE user_id='{$user_id}'");
		if (empty($user_data)){
			Helper_Client::alert("用户不存在");
		}

		if ($_POST){
			$pid = $this->post->pid;
			$user_pms = serialize($pid);
			$info = array(
			"user_pms" => $user_pms,
			);
			$this->db->do_update("tbl_user", $info, "user_id='{$user_id}'");

			// 跳转
			Helper_Client::location("操作成功", "index.php?a=user.list");
		}


		// 选中
		if (!empty($group_id)){
			$group_data = $this->model->group->getInfo($group_id);
			$group_pms  = unserialize($group_data['group_pms']);
			foreach ((array)$group_pms as $key => $value){
				$pms[$value]++;
			}
		}else{
			$user_pms = unserialize($user_data['user_pms']);
			foreach ((array)$user_pms as $key => $value){
				$pms[$value]++;
			}
		}

		// 读出所有的菜单方法
		$pms_lists = $this->model->menu->getAll();
		foreach ($pms_lists as $key => $value){
			if ($pms[$value['menu_id']] && !empty($pms[$value['menu_id']])){
				$pms_lists[$key]['is_check'] = 1;
			}
		}

		$group_lists = $this->model->group->getAll();

		$this->tpl->assign("group_id", $group_id);
		$this->tpl->assign("group_lists", $group_lists);
		$this->tpl->assign("pms_lists", $pms_lists);
		$this->tpl->assign("user_id", $user_id);
		$this->tpl->display("user/user.pms.tpl.php");
	}


}

?>